package cn.memorynine.consumer.service;

import cn.hutool.core.bean.BeanUtil;
import cn.memorynine.common.tool.CommonTool;
import cn.memorynine.consumer.mapper.OrderManageMapper;
import cn.memorynine.consumer.mapper.PersonalCenterMapper;
import cn.memorynine.dao.entity.ShopOrder;
import cn.memorynine.dao.entity.ShopOrderItem;
import cn.memorynine.dao.entity.ShopOrderState;
import cn.memorynine.dao.mapper.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.memorynine.common.model.ResponseWrapper;
import cn.memorynine.consumer.model.order.manage.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* 管理后台-订单管理服务
* @author 代码生成器
*/
@Slf4j
@Service
public class OrderManageService {
    @Autowired
    private ConsumerMapper consumerMapper;

    @Autowired
    private ShopCartCommodityRelationMapper shopCartCommodityRelationMapper;

    @Autowired
    private ShopAddressMapper shopAddressMapper;

    @Autowired
    private ShopOrderMapper shopOrderMapper;

    @Autowired
    private ShopOrderItemMapper shopOrderItemMapper;

    @Autowired
    private ShopOrderStateMapper shopOrderStateMapper;

    @Autowired
    private OrderManageMapper orderManageMapper;

    /**
     * 1.查看所有订单
     * @return
     */
    public ResponseWrapper orderList(OrderListParam param){
        cn.memorynine.consumer.model.personal.order.OrderListResult orderListResult = new cn.memorynine.consumer.model.personal.order.OrderListResult();

        param.setSearchKey("%" + param.getSearchKey() + "%");
        Integer pageNum = (param.getPageNum() - 1) * param.getNum();
        param.setPageNum(pageNum);
        List<Map<String,Object>> orderList = orderManageMapper.getOrderList(param);
        List<cn.memorynine.consumer.model.personal.order.OrderList> orderLists = CommonTool.mapListToBeanList(orderList, cn.memorynine.consumer.model.personal.order.OrderList.class);
        for (cn.memorynine.consumer.model.personal.order.OrderList order : orderLists) {
            ShopOrderItem shopOrderItem = shopOrderItemMapper.selectList(
                    Wrappers.<ShopOrderItem>lambdaQuery()
                            .eq(ShopOrderItem::getOrderAid, order.getAid())
            ).get(0);
            order.setImg(shopOrderItem.getImage());
            order.setName(shopOrderItem.getName());
            order.setPrice(shopOrderItem.getPoints());
        }
        orderListResult.setOrderList(orderLists);
        orderListResult.setTotalNum(orderManageMapper.getOrderListCount(param));

        //最后一步，返回结果
        return ResponseWrapper.markSuccess(orderListResult);
    }

    /**
     * 2.查看订单详情
     * @return
     */
    public ResponseWrapper orderInfo(OrderInfoParam param){
        cn.memorynine.consumer.model.personal.order.OrderInfoResult orderInfoResult = new cn.memorynine.consumer.model.personal.order.OrderInfoResult();

        //1.获取订单基本信息
        List<ShopOrder> shopOrders = shopOrderMapper.selectList(
                Wrappers.<ShopOrder>lambdaQuery()
                        .eq(ShopOrder::getAid, param.getAid())
        );
        if (!shopOrders.isEmpty()){
            ShopOrder shopOrder = shopOrders.get(0);
            BeanUtil.copyProperties(shopOrder,orderInfoResult);
            ShopOrderState shopOrderState = shopOrderStateMapper.selectOne(
                    Wrappers.<ShopOrderState>lambdaQuery().eq(ShopOrderState::getId, shopOrder.getStateId())
            );
            orderInfoResult.setStateName(shopOrderState.getName());
        }


        //2.获取订单项信息
        List<ShopOrderItem> shopOrderItemList = shopOrderItemMapper.selectList(
                Wrappers.<ShopOrderItem>lambdaQuery()
                        .eq(ShopOrderItem::getOrderAid, param.getAid())
        );

        List<cn.memorynine.consumer.model.personal.order.OrderItemList> orderItemLists = new ArrayList<>();
        shopOrderItemList.forEach(item->{
            cn.memorynine.consumer.model.personal.order.OrderItemList orderItemList = new cn.memorynine.consumer.model.personal.order.OrderItemList();
            BeanUtil.copyProperties(item,orderItemList);
            orderItemLists.add(orderItemList);
        });

        orderInfoResult.setOrderItemList(orderItemLists);

        //最后一步，返回结果
        return ResponseWrapper.markSuccess(orderInfoResult);
    }

    /**
     * 3.订单发货
     * @return
     */
    public ResponseWrapper orderDeliver(OrderDeliverParam param){
        ShopOrder shopOrder = new ShopOrder();
        shopOrder.setAid(param.getAid());
        shopOrder.setTimeShipment(LocalDateTime.now());
        shopOrder.setStateId(30);
        shopOrder.setDiliverCustomerAid(param.getTokenConsumerAid());
        shopOrderMapper.updateById(shopOrder);
        //最后一步，返回结果
        return ResponseWrapper.markSuccess();
    }

}